package com.ruoyi.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SnowflakeIdGenerator;
import com.ruoyi.system.domain.CsBuyRecordVo;
import com.ruoyi.system.domain.CsOrnamentName;
import com.ruoyi.system.service.ICsInventoryService;
import com.ruoyi.system.service.ICsOrnamentNameService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CsBuyRecordMapper;
import com.ruoyi.system.domain.CsBuyRecord;
import com.ruoyi.system.service.ICsBuyRecordService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import static com.ruoyi.common.utils.ShiroUtils.getUserId;

/**
 * 物品库存Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-04
 */
@Service
public class CsBuyRecordServiceImpl extends ServiceImpl<CsBuyRecordMapper, CsBuyRecord> implements ICsBuyRecordService {
    @Autowired
    private CsBuyRecordMapper csBuyRecordMapper;

    @Autowired
    private ICsInventoryService iCsInventoryService;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;
    @Autowired
    private ICsOrnamentNameService csOrnamentNameService;

    /**
     * 查询物品库存
     *
     * @param id 物品库存主键
     * @return 物品库存
     */
    @Override
    public CsBuyRecord selectCsBuyRecordById(String id) {
        return csBuyRecordMapper.selectOne(new QueryWrapper<CsBuyRecord>()
                .eq("id", Long.valueOf(id))
                .eq("user_id", getUserId()));
    }

    /**
     * 查询物品库存列表
     *
     * @param csBuyRecord 物品库存
     * @return 物品库存
     */
    @Override
    public List<CsBuyRecord> selectCsBuyRecordList(CsBuyRecord csBuyRecord) {
        return csBuyRecordMapper.selectList(new LambdaQueryWrapper<CsBuyRecord>()
                .eq(CsBuyRecord::getUserId, getUserId())
                .eq(!ObjectUtils.isEmpty(csBuyRecord.getPlatform()), CsBuyRecord::getPlatform, csBuyRecord.getPlatform())
                .eq(!ObjectUtils.isEmpty(csBuyRecord.getAccount()), CsBuyRecord::getAccount, csBuyRecord.getAccount())
                .like(!ObjectUtils.isEmpty(csBuyRecord.getName()), CsBuyRecord::getName, csBuyRecord.getName())
                .orderByDesc(CsBuyRecord::getCreateDate));
    }

    /**
     * 新增物品库存
     *
     * @param csBuyRecord 物品库存
     * @return 结果
     */
    @Override
    public int insertCsBuyRecord(CsBuyRecord csBuyRecord) {
        CsOrnamentName csOrnamentName = csOrnamentNameService.selectCsOrnamentNameByMarketHashName(csBuyRecord.getName());
        if (ObjectUtils.isEmpty(csOrnamentName)) {
            throw new RuntimeException("未找到该饰品:" + csBuyRecord.getMarketHashName());
        }
        csBuyRecord.setName(csOrnamentName.getName());
        csBuyRecord.setMarketHashName(csOrnamentName.getMarketHashName());
        csBuyRecord.setId(snowflakeIdGenerator.nextId());
        csBuyRecord.setUserId(getUserId());
        int i = csBuyRecordMapper.insert(csBuyRecord);
        if (i > 0) {
            // 新增购买记录修改库存表
            csBuyRecord.setUpdateDate(null);
            iCsInventoryService.upsetInventory(csBuyRecord);
        }
        return i;
    }

    /**
     * 修改物品库存
     *
     * @param csBuyRecord 物品库存
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCsBuyRecord(CsBuyRecord csBuyRecord) {
        CsBuyRecord oldBuyRecord = csBuyRecordMapper.selectById(csBuyRecord.getId());
        oldBuyRecord.setUpdateDate(null);
        csBuyRecord.setUpdateDate(null);
        if (oldBuyRecord.getMarketHashName().equals(csBuyRecord.getMarketHashName()) &&
                oldBuyRecord.getAccount().equals(csBuyRecord.getAccount())) {
            // 饰品名称，账户相同，只是修改价格，数量
            iCsInventoryService.updateInventoryByOldBuyRecord(csBuyRecord, oldBuyRecord);
        } else {
            // 饰品名称都直接换掉了
            CsOrnamentName csOrnamentName = csOrnamentNameService.selectCsOrnamentNameByMarketHashName(csBuyRecord.getMarketHashName());
            csBuyRecord.setName(csOrnamentName.getName());
            // 商品名字都修改了,直接对库存减去当前数据
            oldBuyRecord.setQuantity(-oldBuyRecord.getQuantity());
            iCsInventoryService.upsetInventory(oldBuyRecord);
            iCsInventoryService.upsetInventory(csBuyRecord);
        }
        return csBuyRecordMapper.updateById(csBuyRecord);
    }

    /**
     * 批量删除物品库存
     *
     * @param ids 需要删除的物品库存主键
     * @return 结果
     */
    @Override
    public int deleteCsBuyRecordByIds(String ids) {
        return csBuyRecordMapper.deleteCsBuyRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除物品库存信息
     *
     * @param id 物品库存主键
     * @return 结果
     */
    @Override
    public int deleteCsBuyRecordById(String id) {
        return csBuyRecordMapper.deleteCsBuyRecordById(id);
    }
}
